<template>
	<div>
		<div style="float: right;margin-right:2%;">
      <div style="position: relative;display: inline-block;" v-if="isAuth('citys:shezhidaili')">
        <span>省份:</span>
        <el-select v-model="parentIdT" style="width:150px;margin-left: 10px;" @change="handleChange()">
          <el-option v-for="item in citys" :key="item.id" :label="item.cityName" :value="item.id">
          </el-option>
        </el-select>
      </div>
      <div style="position: relative;display: inline-block;" v-if="isAuth('citys:shezhidaili')">
        <span>代理城市:</span>
        <el-select v-model="cityIdT" style="width:150px;margin-left: 10px;" :disabled="parentIdT==''" @change="select()">
          <el-option v-for="item in citys2" :key="item.id" :label="item.cityName" :value="item.id">
          </el-option>
        </el-select>
      </div>
      <el-button style="margin:10px;" size="mini" type="primary" icon="document" @click="select">查询
      </el-button>
      <el-button style="margin:10px;" size="mini" type="primary" icon="document" @click="cleans">重置
      </el-button>&nbsp;&nbsp;
			<el-button style='margin: 10px 0;' :disabled="!isAuth('packageTime:add')" size="mini" type="primary"
				icon="document" @click="addNotice">添加</el-button>
		</div>
		<el-table v-loading="tableDataLoading" :data="tableData.records">

			<el-table-column fixed prop="timeId" label="编号" width="50">
			</el-table-column>
      <el-table-column prop="cityName" label="代理城市" v-if="isAuth('citys:shezhidaili')"></el-table-column>
			<el-table-column prop="timeUnit" label="单位">
        <template slot-scope="scope">
          <span v-if="scope.row.timeUnit==1">分钟</span>
          <span v-if="scope.row.timeUnit==2">小时</span>
          <span v-if="scope.row.timeUnit==3">天</span>
        </template>
			</el-table-column>
			<el-table-column prop="timeNum" label="时长">
			</el-table-column>
      <el-table-column prop="money" label="金额">
      </el-table-column>
      <!-- <el-table-column prop="overTimeMoney" label="超时每分钟价格(单位:元)">
      </el-table-column> -->
			<el-table-column prop="sort" label="排序">
			</el-table-column>
			<el-table-column prop="isEnable" label="是否启用">
				<template slot-scope="scope">
					<el-switch v-model="scope.row.isEnable" @change="change2(scope.row.isEnable,scope.row)"
						:active-value="openValue" :inactive-value="closeValue" active-color="#13ce66"
						inactive-color="#ff4949">
					</el-switch>
				</template>
			</el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160">
      </el-table-column>
			<el-table-column label="操作" width="180">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" :disabled="!isAuth('packageTime:update')"
						@click="amendBanner(scope.row)">修改
					</el-button>
					<el-button size="mini" type="danger" :disabled="!isAuth('packageTime:delete')"
						@click="deletes(scope.row)">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="text-align: center;margin-top: 10px;">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-sizes="[10, 20, 30, 50, 100]" :page-size="limit" :current-page="page"
				layout="total,sizes, prev, pager, next,jumper" :total="tableData.total">
			</el-pagination>
		</div>
		<el-dialog :title="title" :visible.sync="dialogFormVisible" center>
      <div style="margin-bottom: 10px;" v-if="title=='添加'&&isAuth('citys:shezhidaili')">
      	<span style="width: 200px;display: inline-block;text-align: right;">省份：</span>
      	<el-select v-model="parentId" style="width:50%;" @change="handleChange()">
      	  <el-option v-for="item in citys" :key="item.id" :label="item.cityName" :value="item.id">
      	  </el-option>
      	</el-select>
      </div>
      <div style="margin-bottom: 10px;" v-if="title=='添加'&&isAuth('citys:shezhidaili')"> 
      	<span style="width: 200px;display: inline-block;text-align: right;">代理城市：</span>
      	<el-select v-model="cityId" style="width:50%;" :disabled="parentId==''" @change="select()">
      	  <el-option v-for="item in citys2" :key="item.id" :label="item.cityName" :value="item.id">
      	  </el-option>
      	</el-select>
      </div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">单位：</span>
				<el-radio-group v-model="timeUnit">
					<!-- <el-radio :label="1">分钟</el-radio> -->
					<el-radio :label="2">小时</el-radio>
					<!-- <el-radio :label="3">天</el-radio> -->
				</el-radio-group>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">时长：</span>
				<el-input style="width:50%;" v-model="timeNum" placeholder="请输入时长"></el-input>
			</div>
      <div style="margin-bottom: 10px;">
      	<span style="width: 200px;display: inline-block;text-align: right;">价格(单位:元)：</span>
      	<el-input style="width:50%;" v-model="money" placeholder="请输入价格(单位:元)"></el-input>
      </div>
      <!-- <div style="margin-bottom: 10px;">
      	<span style="width: 200px;display: inline-block;text-align: right;">超时每分钟价格(单位:元)：</span>
      	<el-input style="width:50%;" v-model="overTimeMoney" placeholder="请输入超时每分钟价格(单位:元)"></el-input>
      </div> -->
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">排序：</span>
				<el-input style="width:50%;" v-model="sort" placeholder="请输入排序"
					onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')"></el-input>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="addNoticeTo()">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				limit: 10,
				tableDataLoading: true,
				dialogFormVisible: false,
				tableData: [],
				num: '',
				memberImg: '',
				isEnable: 1,
				sort: '',
				timeId: '',
        timeUnit:2,
        timeNum:'',
        money:'',
        overTimeMoney:'',
				title: '添加',
				openValue: 1,
				closeValue: 0,
        parentId: '',
        citys: [],
        citys2: [],
        cityId:'',
        cityIdT:'',
        parentIdT:'',
			}
		},
		methods: {
      // 查询
      select() {
        this.page = 1
        this.limit = 10
        this.dataSelect()
      },
      // 重置
      cleans() {
        this.page = 1
        this.cityIdT = ''
        this.parentIdT = ''
        this.dataSelect()
      },
			// 获取数据列表
			dataSelect() {
				this.tableDataLoading = true
				this.$http({
					url: this.$http.adornUrl('admin/packageTime/getPackageTimeList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page,
						'limit': this.limit,
            'cityId':this.cityIdT,
					})
				}).then(({
					data
				}) => {
					if (data && data.code === 0) {
						console.log('会员列表', data)
						this.tableDataLoading = false
						let returnData = data.data;
						this.tableData = returnData;
						if(this.tableData.records.length==0){
							if(this.page>1){
								this.page = this.page-1
								this.dataSelect()
							}
						}
					}

				})
			},
			//上传成功
			handleAvatarSuccess(file) {
				this.memberImg = file.data
			},
			addNotice() {

        this.timeId = ''
        this.timeUnit = 2
        this.timeNum = ''
        this.moeny = ''
        this.overTimeMoney = ''

				this.sort = ''
				this.isEnable = 1
        this.parentId = ''
        this.cityId = ''
				this.title = '添加'
				this.dialogFormVisible = true
			},
			handleCurrentChange(val) {
				this.page = val
				this.dataSelect()
			},
			handleSizeChange(val) {
				this.limit = val
				this.dataSelect()
			},
			// 添加banner图
			addNoticeTo() {
				// if (this.timeUnit === '') {
				// 	this.$notify({
				// 		title: '提示',
				// 		duration: 1800,
				// 		message: '请选择单位',
				// 		type: 'warning'
				// 	});
				// 	return
				// }
				if (this.timeNum === '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入时长',
						type: 'warning'
					});
					return
				}
        if (this.money === '') {
        	this.$notify({
        		title: '提示',
        		duration: 1800,
        		message: '请输入价格(单位:元)',
        		type: 'warning'
        	});
        	return
        }
        // if (this.overTimeMoney === '') {
        // 	this.$notify({
        // 		title: '提示',
        // 		duration: 1800,
        // 		message: '请输入超时每分钟价格(单位:元)',
        // 		type: 'warning'
        // 	});
        // 	return
        // }

				this.classify = 1
				if (this.title == '添加') {
					var url = 'admin/packageTime/savePackageTime'
				} else {
					var url = 'admin/packageTime/savePackageTime'
				}
				this.$http({
					url: this.$http.adornUrl(url),
					method: 'post',
					params: this.$http.adornParams({
					// data: this.$http.adornData({
						'timeId': this.timeId,
						'timeUnit': this.timeUnit,
						'timeNum': this.timeNum,
						'money': this.money,
						'overTimeMoney': this.overTimeMoney,
						'isEnable': this.isEnable,
						'sort': this.sort,
            'cityId':this.cityId,

					})
				}).then(({
					data
				}) => {
					if(data.code==0){
						this.dialogFormVisible = false
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {

								this.dataSelect()
							}
						})
					}else{
						this.$message({
							message: data.msg,
							type: 'warning',
							duration: 1500,
							onClose: () => {
							}
						})
					}

				})
			},
			// 修改
			amendBanner(rows) {

				this.dialogFormVisible = true
				this.timeId = rows.timeId
				this.timeUnit = 2
				this.timeNum = rows.timeNum
				this.money = rows.money
				this.overTimeMoney = rows.overTimeMoney
				this.sort = rows.sort
				this.isEnable = rows.isEnable
        this.cityId = rows.cityId
				this.title = '修改'
			},
			//删除一级
			deletes(row) {
				let delid = row.timeId
				this.$confirm(`确定删除此条信息?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: ''
				}).then(() => {
					this.$http({
						url: this.$http.adornUrl('admin/packageTime/deletePackageTimeById'),
						method: 'post',
						params: this.$http.adornParams({
							'timeId':delid
						})
					}).then(({
						data
					}) => {
						if(data.code==0){
							this.$message({
								message: '删除成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.dataSelect()
								}
							})
						}else{
							this.$message({
								message: data.msg,
								type: 'warning',
								duration: 1500,
								onClose: () => {
								}
							})
						}

					})
				}).catch(() => {})
			},
			// 是否启用
			change2(val, row) {
				this.$http({
					url: this.$http.adornUrl('admin/packageTime/savePackageTime'),
					method: 'post',
					params: this.$http.adornParams({
					// data: this.$http.adornData({
						'timeId': row.timeId,
						'timeUnit': 2,
						'timeNum': row.timeNum,
						'money': row.money,
						'overTimeMoney': row.overTimeMoney,
						'isEnable': row.isEnable,
						'sort': row.sort,
					})
				}).then(({
					data
				}) => {
					if(data.code==0){
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					}else{
						this.$message({
							message: data.msg,
							type: 'warning',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					}
				})
			},
      // 获取城市省份列表
      dataSelectCity() {
        this.$http({
          url: this.$http.adornUrl('admin/cityInfo/getCityInfoList'),
          method: 'get',
          params: this.$http.adornParams({
            'parentId': 0,
            'cityName': '',
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            let returnData = data.data;
            this.citys = returnData.records
          }

        })
      },
      // 获取城市
      dataSelectCity2() {
        this.$http({
          url: this.$http.adornUrl('admin/cityInfo/getCityInfoList'),
          method: 'get',
          params: this.$http.adornParams({
            'parentId': this.parentId,
            'cityName': '',
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.citys2 = data.data.records
          }

        })
      },
      handleChange() {
        this.cityId = ''
        this.cityIdT = ''
        if(this.parentIdT!=''){
          this.parentId = this.parentIdT
        }
        this.dataSelectCity2()
      },
		},
		mounted() {
       this.parentId = ''
       this.parentIdT = ''
			this.dataSelect()
      this.dataSelectCity()
		}
	}
</script>

<style>
</style>
